package it.polimi.swKnights.SWIMv2.servlets.abilities;

import it.polimi.swKnights.SWIMv2.ejb.abilityManager.AbilityHandlerRemote;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.AbilityNotFoundException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.general.Errors;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AddAbilityController
 */
public class AddAbilityController extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public AddAbilityController()
	{

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		AbilityHandlerRemote abilityManager = this
				.lookUp(AbilityHandlerRemote.class);
		String ability = request.getParameter("toAddAbility");
		ability = (ability == null) ? "" : ability;
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");

		try
		{
			abilityManager.addAbility(username, password, ability);
			response.sendRedirect("./userHome?message="
					+ "Ability added succesfully");

		}
		catch (LoginFailException e)
		{
			response.sendRedirect("./siteHome?error=" + Errors.session);
		}
		catch (InvalidLoginPrivilegesException e)
		{
			response.sendRedirect("./siteHome?error=" + Errors.notuser);
		}
		catch (AbilityNotFoundException e)
		{
			response.sendRedirect("./userHome?error=" + Errors.noability);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{

	}

}
